package com.genexususa.eo.bing.data.wsSubclasses;

import com.genexususa.eo.bing.CustomerManagementServiceClasses.*;
import com.genexususa.eo.bing.data.GxItemException;
import com.genexususa.eo.bing.data.Util;
import org.apache.axis.types.UnsignedByte;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class GxAccount extends Account {
    private long gxId;

    public GxAccount(Element element) throws IllegalArgumentException, GxItemException {
        if (element.getElementsByTagName("AccountType").getLength() > 0 &&
            element.getElementsByTagName("AccountType").item(0).getChildNodes().getLength() > 0) {
            this.setAccountType(AccountType.fromString(((Node) element.getElementsByTagName("AccountType").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("BillToCustomerId").getLength() > 0 &&
            element.getElementsByTagName("BillToCustomerId").item(0).getChildNodes().getLength() > 0) {
            this.setBillToCustomerId(Long.valueOf(((Node) element.getElementsByTagName("BillToCustomerId").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("CountryCode").getLength() > 0 &&
            element.getElementsByTagName("CountryCode").item(0).getChildNodes().getLength() > 0) {
            this.setCountryCode(((Node) element.getElementsByTagName("CountryCode").item(0).getChildNodes().item(0)).getNodeValue());
        }
        if (element.getElementsByTagName("CurrencyType").getLength() > 0 &&
            element.getElementsByTagName("CurrencyType").item(0).getChildNodes().getLength() > 0) {
            this.setCurrencyType(CurrencyType.fromString(((Node) element.getElementsByTagName("CurrencyType").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("AccountFinancialStatus").getLength() > 0 &&
            element.getElementsByTagName("AccountFinancialStatus").item(0).getChildNodes().getLength() > 0) {
            this.setAccountFinancialStatus(AccountFinancialStatus.fromString(((Node) element.getElementsByTagName("AccountFinancialStatus").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("GxAccountId").getLength() > 0 &&
            element.getElementsByTagName("GxAccountId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("GxAccountId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.gxId = Long.valueOf(((Node)element.getElementsByTagName("GxAccountId").item(0).getChildNodes().item(0)).getNodeValue());
        } else {
            throw new GxItemException("Missing external Account id");
        }
        if (element.getElementsByTagName("BingAccountId").getLength() > 0 &&
            element.getElementsByTagName("BingAccountId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("BingAccountId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.setId(Long.valueOf(((Node) element.getElementsByTagName("BingAccountId").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("Language").getLength() > 0 &&
            element.getElementsByTagName("Language").item(0).getChildNodes().getLength() > 0) {
            this.setLanguage(LanguageType.fromString(((Node) element.getElementsByTagName("Language").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("LastModifiedByUserId").getLength() > 0 &&
            element.getElementsByTagName("LastModifiedByUserId").item(0).getChildNodes().getLength() > 0) {
            this.setLastModifiedByUserId(Long.valueOf(((Node) element.getElementsByTagName("LastModifiedByUserId").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        String dateString = null;
        Util util = new Util();
        if (element.getElementsByTagName("LastModifiedTime").getLength() > 0 &&
            element.getElementsByTagName("LastModifiedTime").item(0).getChildNodes().getLength() > 0) {
            dateString = ((Node) element.getElementsByTagName("LastModifiedTime").item(0).getChildNodes().item(0)).getNodeValue();
            if (dateString == null || dateString.equals("")) {
                try {
                    this.setLastModifiedTime(util.calendarFromString(dateString));
                } catch (GxItemException e) {
                    throw new GxItemException("LastModifiedTime format is invalid");
                }
            }
        }
        if (element.getElementsByTagName("Name").getLength() > 0 &&
            element.getElementsByTagName("Name").item(0).getChildNodes().getLength() > 0) {
            this.setName(((Node)element.getElementsByTagName("Name").item(0).getChildNodes().item(0)).getNodeValue());
        }
        if (element.getElementsByTagName("Number").getLength() > 0 &&
            element.getElementsByTagName("Number").item(0).getChildNodes().getLength() > 0) {
            this.setNumber(((Node)element.getElementsByTagName("Number").item(0).getChildNodes().item(0)).getNodeValue());
        }
        if (element.getElementsByTagName("ParentCustomerId").getLength() > 0 &&
            element.getElementsByTagName("ParentCustomerId").item(0).getChildNodes().getLength() > 0) {
            this.setParentCustomerId(Long.valueOf(((Node)element.getElementsByTagName("ParentCustomerId").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("PaymentMethodId").getLength() > 0 &&
            element.getElementsByTagName("PaymentMethodId").item(0).getChildNodes().getLength() > 0) {
            this.setPaymentMethodId(Long.valueOf(((Node)element.getElementsByTagName("PaymentMethodId").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("PaymentMethodType").getLength() > 0 &&
            element.getElementsByTagName("PaymentMethodType").item(0).getChildNodes().getLength() > 0) {
            this.setPaymentMethodType(PaymentMethodType.fromString(((Node)element.getElementsByTagName("PaymentMethodType").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("PrimaryUserId").getLength() > 0 &&
            element.getElementsByTagName("PrimaryUserId").item(0).getChildNodes().getLength() > 0) {
            this.setPrimaryUserId(Long.valueOf(((Node)element.getElementsByTagName("PrimaryUserId").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("AccountLifeCycleStatus").getLength() > 0 &&
            element.getElementsByTagName("AccountLifeCycleStatus").item(0).getChildNodes().getLength() > 0) {
            this.setAccountLifeCycleStatus(AccountLifeCycleStatus.fromString(((Node)element.getElementsByTagName("AccountLifeCycleStatus").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        NodeList timeStampsNode = element.getElementsByTagName("TimeStamp");
        if (timeStampsNode.getLength() > 0) {
            NodeList timeStampNode = ((Element)timeStampsNode.item(0)).getElementsByTagName("TimeStampItem");
            byte[] timeStamps = new byte[timeStampNode.getLength()];
            for (int i=0; i<timeStampNode.getLength(); i++) {
                if (timeStampNode.item(i).getChildNodes().getLength() > 0) {
                    timeStamps[i] = Byte.valueOf(timeStampNode.item(i).getChildNodes().item(0).getNodeValue());
                }
            }
            this.setTimeStamp(timeStamps);
        }
        if (element.getElementsByTagName("TimeZone").getLength() > 0 &&
            element.getElementsByTagName("TimeZone").item(0).getChildNodes().getLength() > 0) {
            this.setTimeZone(TimeZoneType.fromString(((Node) element.getElementsByTagName("TimeZone").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("PauseReason").getLength() > 0 &&
            element.getElementsByTagName("PauseReason").item(0).getChildNodes().getLength() > 0) {
            this.setPauseReason(new UnsignedByte(((Node) element.getElementsByTagName("PauseReason").item(0).getChildNodes().item(0)).getNodeValue()));
        }
    }

    public long getGxId() {
        return gxId;
    }

    public void setGxId(long gxId) {
        this.gxId = gxId;
    }
}
